package com.an.threadtest.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ThreadPoolCallableFuture {
	public static void main(String[] args) {
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		Future<String> future = threadPool.submit(new Callable<String>() {
			@Override
			public String call() throws Exception {
				// 当前线程休眠1秒
				Thread.sleep(1000);
				return "hello,langsin!";
			}
		});
		try {
			String s = future.get(2, TimeUnit.SECONDS);
			System.out.println("线程的返回值是:" + s);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		} catch (TimeoutException e) {
			System.out.println("超时！");
			e.printStackTrace();
		}
	}

}
